package com.wu.demo.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class ByteUtil {

	public static char[] getChars(byte[] bytes) {
		Charset cs = Charset.forName("UTF-8");
		ByteBuffer bb = ByteBuffer.allocate(bytes.length);
		bb.put(bytes);
		bb.flip();
		CharBuffer cb = cs.decode(bb);
		return cb.array();
	}

	public static String toHex(int by) {
		StringBuffer s = new StringBuffer();
		String a;
		char[] b = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
				'B', 'C', 'D', 'E', 'F' };
		while (by != 0) {
			s = s.append(b[by % 16]);
			by = by / 16;
		}
		a = s.reverse().toString();
		if (StringUtils.isEmpty(a)) {
			a = "00";
		}
		return a;
	}

	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	public static int byteArrayToInt(byte[] bytes) {
		int value = 0;
		// 由高位到低位
		// for (int i = 0; i < 4; i++) {
		// int shift = (4 - 1 - i) * 8;
		// value += (bytes[i] & 0x000000FF) << shift;// 往高位游
		// }
		value = bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3];
		return value;
	}

	public static int byte2Int(byte b) {
		return (int) (b & 0xff);
	}

	public static int byte2int(byte[] b) {
		int res = 0;
		int bLen = b.length;

		if (bLen < 5) {// int 最大到4个字节
			for (int i = 0; i < bLen; i++) {
				res += (b[i] & 0xFF) << (8 * i);
			}
		}

		return res;
	}

	/**
	 * 字符串转化成为16进制字符串
	 * 
	 * @param s
	 * @return
	 */
	public static String strTo16(String s) {
		String str = "";
		for (int i = 0; i < s.length(); i++) {
			int ch = (int) s.charAt(i);
			String s4 = Integer.toHexString(ch);
			str = str + s4;
		}
		return str;
	}
	
	public static int hex2int(String hexstr){
		return Integer.valueOf(hexstr, 16);
	}
	
	public static float hex2Float(byte[] bytes){
		float result = 0;
		String hexstr = bytesToHexString(bytes);
		Long resultLong = Long.parseLong(hexstr, 16);
		result = Float.intBitsToFloat(resultLong.intValue());
		return result;
	}
	
	public static byte[] parseHexStr2Byte(String hexStr) {
		if (hexStr.length() < 1)
			return null;
		byte[] result = new byte[hexStr.length() / 2];
		for (int i = 0; i < hexStr.length() / 2; i++) {
			int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
			int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
			result[i] = (byte) (high * 16 + low);
		}
		return result;
	}

	public static void main(String[] args) {
		String dd = "06";
		byte[] buffer = parseHexStr2Byte(dd);
		System.out.println(buffer.length);
		for(byte b : buffer){
			int ib = byte2Int(b);
			System.out.println(String.format("%04d",Integer.valueOf(Integer.toBinaryString(ib))));
		}
	}
}
